package com.taiji.dianthus.service.sms.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.dao.api.SmsPublicUseRepository;
import com.taiji.dianthus.dao.sms.SmsBackInfoRepository;
import com.taiji.dianthus.domain.app.AppInfo;
import com.taiji.dianthus.domain.sms.SmsBackInfo;
import com.taiji.dianthus.domain.sms.SmsInfo;
import com.taiji.dianthus.domain.sms.api.SmsDomain;
import com.taiji.dianthus.service.app.AppInfoService;
import com.taiji.dianthus.service.sms.SmsBackInfoService;
import com.taiji.dianthus.service.sms.SmsInfoService;
import com.taiji.dianthus.util.RestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @ClassName SmsInfoServiceImnpl
 * @Description
 * @Author H.M
 * @Date 2020/7/15
 */
@Service
@Transactional
public class SmsBackInfoServiceImpl implements SmsBackInfoService {

    @Autowired
    private SmsBackInfoRepository repository;
    @Autowired
    private SmsInfoService smsInfoService;

    @Autowired
    private SmsPublicUseRepository publicUseRepository;
    @Autowired
    private AppInfoService appInfoService;
    @Autowired
    private RestUtil restUtil;

    @Override
    public List<SmsBackInfo> getBySmsGroup(String smsGroup) {
        List<SmsBackInfo> backInfos = null;
        try {
            SmsInfo smsInfo = smsInfoService.getOneByMsgGroup(smsGroup);
            backInfos = repository.getBySmsGroup(smsGroup);
            String[] userPhones = smsInfo.getReceiveUserPhone().split(",");
            if (backInfos.size() < userPhones.length) {
                for (int i = 0; i < userPhones.length; i++) {
                    boolean flag = false;
                    for (int j = 0; j < backInfos.size(); j++) {
                        SmsBackInfo backInfo = backInfos.get(j);
                        if (backInfo.getMobile().equals(userPhones[i])) {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        SmsBackInfo backInfo = new SmsBackInfo();
                        backInfo.setMobile(userPhones[i]);
                        backInfo.setErrorCode("UNKNOWN");
                        backInfos.add(backInfo);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return backInfos;
    }

    @Override
    public BusinessMessage saveBackInfo(SmsBackInfo smsBackInfo) {
        BusinessMessage message = new BusinessMessage();
        String errorCode = smsBackInfo.getErrorCode();
        try {
            SmsInfo smsInfo = smsInfoService.getOneByMsgGroup(smsBackInfo.getMsgGroup());
            if (smsInfo == null) {
                //在外部调用信息表查询  将回执推送给外部url
                String msgGroup = smsBackInfo.getMsgGroup();
                if (!StringUtils.isEmpty(msgGroup)) {
                    msgGroup = "%" + msgGroup + "%";
                }
                SmsDomain domain = publicUseRepository.getOneByGroup(msgGroup);
                if (domain != null) {
                    AppInfo appInfo = appInfoService.getAppInfoByAppId(domain.getAppId());
                    try {
                        if (appInfo != null && !StringUtils.isEmpty(appInfo.getStatusReportUrl())) {
                            restUtil.post(appInfo.getStatusReportUrl(), JSONObject.parseObject(JSON.toJSONString(smsBackInfo)));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else {
                smsBackInfo.setSmsInfoId(smsInfo.getId());
                Integer status = smsInfo.getMsgStatus();//本系统自己调用
                repository.saveAndFlush(smsBackInfo);
                System.out.println("===更新发送短信状态开始====");
                if (!StringUtils.isEmpty(errorCode) && "DELIVRD".equals(errorCode)) {
                    if (status == 0) {
                        //之前状态是发送中
                        smsInfoService.updateStatusByGroup(smsBackInfo.getMsgGroup(), 1);
                    } else if (status == 1) {
                        //之前状态是发送成功
                        smsInfoService.updateStatusByGroup(smsBackInfo.getMsgGroup(), 1);
                    } else if (status == 2) {
                        //之前状态是发送失败
                        smsInfoService.updateStatusByGroup(smsBackInfo.getMsgGroup(), 3);
                    }
                } else {
                    if (status == 0) {
                        //之前状态是发送中
                        smsInfoService.updateStatusByGroup(smsBackInfo.getMsgGroup(), 2);
                    } else if (status == 1) {
                        //之前状态是发送成功
                        smsInfoService.updateStatusByGroup(smsBackInfo.getMsgGroup(), 3);
                    } else if (status == 2) {
                        //之前状态是发送失败
                        smsInfoService.updateStatusByGroup(smsBackInfo.getMsgGroup(), 2);
                    }
                }
                message.setSuccess(true);
                message.setMsg("保存回执信息成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            message.setMsg("保存回执信息失败");
        }
        return message;
    }
}
